package com.jplus.service.impl;

import com.jplus.VO.ResultVO;
import com.jplus.dao.CommentsDao;
import com.jplus.dao.TopicDao;
import com.jplus.dto.CommentDetailsDTO;
import com.jplus.entity.Comments;
import com.jplus.entity.Topic;
import com.jplus.entity.User;
import com.jplus.enums.ResultEnum;
import com.jplus.exception.SFUmbrellaException;
import com.jplus.service.CommentService;
import com.jplus.utils.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
/**
 * GlobalExceptionHandler 统一异常处理
 */
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentsDao commentsDao;

    @Autowired
    private TopicDao topicDao;

    @Override
    public ResultVO submitComment(Comments comments, Topic topic, User user) {
        // 1.整合即将提交到comment表的数据
       // comments.setFromUid(user.getUserId());
        // 2.提交数据到各个表里面
        try {
            //  先提交topic，TODO [添加新话题]之前判断是否已经存在此topic
            //topicDao.insertOneTopic(topic);

            topic = topicDao.getOneTopicByTopicContent(topic.getTopicContent());

            // 完善comment TODO 不允许提交已经存在的评论
            comments.setTopicId(topic.getTopicId());

            if (commentsDao.getOneCommentByContent(comments.getCommentContent()) != null) {
                //数据库已经存在这条评论了，则不允许评论
                //log.error("禁止当前用户：{}重复评论：{}，时间：{}", user.getUsername(), comments.getCommentContent(), new Date());
                throw new SFUmbrellaException(ResultEnum.COMMENT_REPEAT_FORBIDDEN);
            } else {
                commentsDao.insertOneComment(comments);
                comments = commentsDao.getOneCommentByContent(comments.getCommentContent());
                // 在DTO里面封装数据
                CommentDetailsDTO commentDetailsDTO = new CommentDetailsDTO();
                commentDetailsDTO.setComments(comments);
                commentDetailsDTO.setTopic(topic);
                commentDetailsDTO.setUser(user);
                //log.info("当前用户：{}评论：{}成功", user.getUsername(), comments.getCommentContent());
                return ResultVOUtils.success(commentDetailsDTO);
            }
        } catch (Exception e) {
            //log.error("用户：{}评论过程中发生异常：{}", user.getUsername(), e.getMessage());
            throw new SFUmbrellaException(ResultEnum.ORDER_CREATE_ERROR);
        }
    }

    @Override
    public ResultVO getTopicBeforeGoToCommentPage() {
        try {
            List<Topic> topics = topicDao.getAllTopic();
            if (topics != null) {
                log.info("加载全部话题成功");
                return ResultVOUtils.success(topics);
            } else {
                log.error("加载全部话题失败");
                return ResultVOUtils.error(0, "加载全部话题失败");
            }
        } catch (Exception e) {
            log.error("话题预加载失败：{}", e.getMessage());
            throw new SFUmbrellaException(ResultEnum.TOPIC_LODING_ERROR);
        }
    }
}
